from .models import *
from userapp.models import User
from rest_framework import serializers


# 定义一个 用户信息输出序列化

class UserInfoSer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "username", "phone", "email"]


#  定义一个 城市序列化类

class ChinaCitySer(serializers.ModelSerializer):
    class Meta:
        model = ChinaCity
        fields = ["areaid", 'areaname', 'parentid']


# 定义一个 地址序列化类

class AddressSer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = "__all__"


class AddressListSer(serializers.ModelSerializer):
    province = serializers.SerializerMethodField()
    city = serializers.SerializerMethodField()
    district = serializers.SerializerMethodField()

    def get_province(self, address):
        return ChinaCity.objects.get(areaid=address.province).areaname

    def get_city(self, address):
        return ChinaCity.objects.get(areaid=address.city).areaname

    def get_district(self, address):
        return ChinaCity.objects.get(areaid=address.district).areaname

    class Meta:
        model = Address
        fields = "__all__"
